<div class="tableContent whiteBg moneyBomb" style=" width: 560px; min-height: 360px; padding: 10px; margin: auto; margin-top:10px;">
    <div class="pd20" style="height:auto;background: #FFFFFF;">
        <ul class="list-unstyled list-justify" style=" max-height: 280px; overflow-y: auto;" id="pay_order_list"></ul>
        <div class="sPaymentBox ftc">
            <div class="green ftl">应收 <span class="r" id="pay_order_amount">0</span></div>
            <div class="tit1 bcb ftl mt10 mb10">支付方式</div>
            <table align="center" width="90%" id="pay_tab">
                <tr>
                    <td width="50%">

                        <table class="table" style="border: none;">
                            <tr>
                                <td width="100">现金支付:</td>
                                <td><input type="text" class="form-control" placeholder="" value="0" id="pay_cash"
                                           onkeyup="value=value.replace(/[^\0-9.]/g,'')"
                                           onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\0-9.]/g,''))"
                                           /></td>
                            </tr>
                            <tr>
                                <td>在线支付:</td>
                                <td><input type="text" class="form-control" placeholder="" value="0" id="pay_ol_input"
                                           onkeyup="value=value.replace(/[^\0-9.]/g,'')"
                                           onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\0-9.]/g,''))"
                                           />
                                    </td>
                            </tr>
                        </table>

                    </td>
                    <td width="50%" id="ol_result_box">
                        <img src="/Public/his/img/load_icon.gif" id="pay_qr_img"/>
                        <div>
                            <img src="/Public/assets/img/wx_ali.jpg" width="215" />
                        </div>
                    </td>
                </tr>
            </table>

        </div>

    </div>
    <div class="bottomPageBox" style="position:inherit;">
    <button type="button" class="btn btn-primary wb100" id="btn_save_pay">保 存</button>
        <div style="text-align: right;padding: 10px;"><a href="/Doctor/printPay/pkg_id/{$pkg.id}" onclick="window.print();return false;" target="_blank">打印</a></div>
</div>
</div>
<style type="text/css" media=print>
    .noprint{display : none }
</style>


<style>
    .tr_registration_item, .tr_patient_list_item, .tr_medicines_list_item, .tr_inspection_list_item, .tr_extracharges_list_item, .history_list_item {
        cursor: pointer;
    }

    #registration_pager_box, #patient_list_pager_box {
        text-align: center;
        width: 100%;
    }

    #registration_box .on, #patient_list_box .on, #medicines_list_box .on, #inspectionfee_list_box .on, #extracharges_list_box .on, #history_search_list_box .on {
        background: #1a8cff;
        color: #FFFFFF;
    }

    #medicines_list_box .sel, #inspectionfee_list_box .sel, #extracharges_list_box .sel {
        background: #0CA818;
        color: #FFFFFF;
    }

    .care_order_sub_box {
        border: 1px solid #0E3460;
        min-height: 50px;
        padding: 12px;
        margin-bottom: 12px;
    }

    .care_order_sub_box span {
        display: inline-block;
        padding: 5px;
        margin-right: 2px;
    }

    .medicines_show {
        display: none;
    }

    .pay_status {
        padding: 6px 8px;
    }

    .unpay {
        background: #7C7C7C;
        color: #FF0000;
    }

    .payed {
        background: #0CA818;
        color: #FFFFFF;
    }

    .form-group{line-height: 35px;height: 35px;}

    #pay_tab td{border: none;}

    body{background: #FFFFFF;}
</style>
<!-- END WRAPPER -->
<script>

    var DATA = {
        //患者信息
        'patient': {
            patient_id: 0,
            name: '',
            mobile: '',
            sex: 1,
            birthday: '',
            address: '',
            allergy_info: ''
        },
        //患者病历
        'history': {
            id: 0,
            type_id: 0,
            is_contagious: 0,
            case_date: '',
            case_code: '',
            case_title: '',
            case_result: '',
            doctor_tips: '',
            memo: ''
        },
        //药品及治疗项目
        care_order: [],
        pkg_id: {$pkg.id},
        pkg_status: {$pkg.status},
        pkg_amount: {$pkg.amount},
        pkg_order_code: {$pkg.order_code},
        registration_id: 0
    };


    $(function () {


        //更新在线支付额度

        $("#pay_cash").on('input propertychange',function () {
            var val = $(this).val();
            if(val.length==0){
                $("#pay_ol_input").val(DATA.pkg_amount.toFixed(2));
                update_ol_amount();
                return false;
            }
            var ol = DATA.pkg_amount-parseFloat(val);
            if(ol<0){
                ol=0;
                $("#pay_cash").val(DATA.pkg_amount.toFixed(2));
            }
            $("#pay_ol_input").val(ol.toFixed(2));
            update_ol_amount();
        });

        $("#pay_ol_input").on('input propertychange',function () {
            var val = $(this).val();
            if(val.length==0){
                $("#pay_cash").val(DATA.pkg_amount.toFixed(2));
                update_ol_amount();
                return false;
            }
            var cash = DATA.pkg_amount-parseFloat(val);
            if(cash<0){
                cash=0;
                $("#pay_ol_input").val(DATA.pkg_amount.toFixed(2));
            }
            $("#pay_cash").val(cash.toFixed(2));
            update_ol_amount();
        });

        //保存订单
        $("#btn_save_pay").click(function () {
            var cash = $("#pay_cash").val();
            var ol = $("#pay_ol_input").val();

            if(ol.length==0)ol=0;
            if(cash.length==0)cash=0;


            if(parseFloat(ol)+parseFloat(cash)<DATA.pkg_amount){
                //console.log('ol:%o,cash:%o,amount:%o',ol,cash,DATA.pkg_amount);
                return false;
            }

            $.post('/Doctor/payOrder',{pkg_id:DATA.pkg_id,ol:ol,cash:cash,pkg_status:DATA.pkg_status,registration_id:DATA.registration_id},function (res) {
                if(res.status==0){
                    //if(parent.pay_back) {
                        parent.pay_back(DATA.pkg_id);
                    /*}else{
                        pay_back(DATA.pkg_id);
                    }*/


                    remindBox('保存成功');

                    stopPayStatus=1;
                    DATA.pkg_status=1;
                    DATA.pkg_order_code=res.data.order_code;
                    show_pay_status();
                }else{
                    alert(res.msg);
                }
            });

        });

        getMain();

        if(parent.DATA)parent.DATA = DATA;
    });


    function update_ol_amount() {
        var ol = $("#pay_ol_input").val();
        if(ol.length==0)ol=0;
        $.post('/Doctor/change_ol_pay_part',{pkg_id:DATA.pkg_id,ol:ol},function (res) {
            if(res.status==0){
                //remindBox('更新成功');
            }else{
                alert(res.msg);
            }
        });
    }

    function cash_all() {
        var ol = parseFloat($("#pay_ol_input").val());
        var c = parseFloat($("#pay_cash").val());
        if(ol>0){
            $("#pay_ol_input").val(0);
            $("#pay_cash").val(ol);
        }else{
            $("#pay_ol_input").val(c);
            $("#pay_cash").val(0);
        }

        update_ol_amount();

    }


    //显示收费状态
    function show_pay_status() {

        if (DATA.pkg_status == 1) {
            $("#pay_qr_img").hide();
        } else {
            $("#pay_qr_img").show();
        }
    }


    //获取收费信息
    function getMain() {
        $.post('/Doctor/getOrder', {pkg_id: DATA.pkg_id}, function (res) {
            //closeLoadBox();
            if (res.status == 0) {
                var d = res.data;
                DATA.pkg_status = res.data.pkg.status;
                DATA.pkg_order_code = res.data.pkg.order_code;

                if (d.num > 0) {
                    var all_amount = 0, txt = '';
                    $.each(d.list, function (i, n) {
                        all_amount += parseFloat(n.all_amount);

                        txt += '<li>' + n.label + ' <span class="r">' + n.all_amount + '元</span></li>'

                    });

                    if (all_amount > 0) {
                        $("#pay_order_list").html('<li>收费信息 <span class="r">门诊编号: ' + res.data.pkg.order_code + '</span></li>' + txt);
                        $("#pay_order_amount").html(all_amount.toFixed(2) + '元');

                        $("#pay_ol_input").val(res.data.pkg.ol_pay_part);

                        var cash = res.data.pkg.amount - res.data.pkg.ol_pay_part;

                        $("#pay_cash").val(cash);

                        DATA.pkg_amount = all_amount;

                        //支付二维码
                        $("#pay_qr_img").attr('src', '/qr?size=5&type=pay&id=' + DATA.pkg_id);
                        getMainSub();
                    } else {
                        remindBox('暂无收费项目，无法收费');
                    }
                }

            } else {
                alert(res.msg);
            }
        });


    }

    //显示收费弹窗
    function getMainSub() {
        stopPayStatus = 0;
        $('#moneyBomb').fadeIn();
        // 取消或者关闭
        $('#moneyBomb .bombMask, #moneyBomb .fa-remove').one('click', function (event) {
            $(this).closest('#moneyBomb').fadeOut();
            $('body').removeAttr('style');
            stopPayStatus = 1;
        });

        getOnLinePay();
    }

    //获取在线支付状态
    function getOnLinePay() {
        if (stopPayStatus != 0) return false;
        $.post('/Doctor/getOnLinePay', {pkg_id: DATA.pkg_id, pkg_status: DATA.pkg_status}, function (res) {
            if (res.status == 0) {

                DATA.pkg_status = res.data.pkg.status;
                DATA.pkg_order_code = res.data.pkg.order_code;

                show_pay_status();

                if (res.data.num > 0) {
                    var all_amount = 0, type;
                    $("#pay_ol_input").val(0);

                    $.each(res.data.list, function (i, n) {
                        all_amount += parseFloat(n.pay_amount);
                        switch (n.payment_platform) {
                            case '0':
                                $("#pay_cash").val(n.pay_amount);
                                $("#btn_save_pay").hide();
                                $("#pay_cash").attr('disabled', true);
                                break;
                            default:
                                $("#pay_ol_input").val(n.pay_amount);
                                var ss='<img src="/Public/assets/img/pay_done_'+n.payment_platform+'.png" />\n' +
                                    '<div>'+n.payment_platform_label+'到账:￥<span  style="color: #FF0000;font-size: 16px;font-weight: bold;">'+
                                    n.pay_amount+'</span></div>';
                                $('#ol_result_box').html(ss);

                                $("#pay_ol_input").attr('disabled', true);
                                $("#pay_qr_img").hide();

                        }

                        //type = n.payment_platform_label;

                    });

                    if(all_amount>=res.data.pkg.amount){
                        $("#btn_save_pay").hide();
                    }else{

                        var cash = res.data.pkg.amount-all_amount;
                        $("#pay_cash").val(cash);

                    }




                    //var cash = DATA.pkg_amount-parseFloat(all_amount);
                    /*if(cash<0){
                        cash=0;
                        $("#pay_ol_input").val(DATA.pkg_amount.toFixed(2));
                    }*/

                    //$("#pay_ol_input").val(all_amount);

                    // $("#pay_cash").val(cash.toFixed(2));


                    //if(all_amount>=DATA.pkg_amount){
                    // $("#pay_ol_input").attr('disabled',true);
                    //}else{

                    ///}


                } else {
                    $("#btn_save_pay").show();
                    show_pay_status();
                    if (DATA.pkg_status == 0) setTimeout(getOnLinePay, 3000);
                }
            } else {
                alert(res.msg);
            }
        });
    }


</script>
